﻿using Inovout.Account.Web.OpenIdClients;
using Inovout.Runtime;
using Inovout.Web;
using Inovout.Web.Security;
using Microsoft.Web.WebPages.OAuth;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;

namespace Inovout.Account.Web
{
    public class MvcApplication : WebApplication
    {
        internal static string UserIdentifierRouteName = "UserIdentifier";
        internal static string OpenIdIdentifierRouteName = "OpenIdIdentifier";

        protected override void RegisterApplicationRoutes(RouteCollection routes)
        {
            routes.MapRoute(
                      UserIdentifierRouteName,
                        "user/{name}",
                       new { controller = "User", action = "Identifier", name = string.Empty });
            routes.MapRoute(
                OpenIdIdentifierRouteName,
                "OpenId/{action}",
                new { controller = "OpenId", action = "Identifier" });

            routes.MapRoute(
                name: "OpenIdSignIn",
                url: "Account/SignIn/{provider}",
                defaults: new { controller = "Account", action = "SignIn", provider = UrlParameter.Optional },
                namespaces: new string[] { "Inovout.Account.Web.Controllers" });

            base.RegisterApplicationRoutes(routes);
        }

        protected override void RegisterAuthenticationClient()
        {
            //#if DEBUG
            //OAuthWebSecurity.RegisterClient(new MailAccountClient("http://mailaccount.3cloud.dev/OpenId?force_login=1"), "三宝邮箱", new Dictionary<string, object>());
            ////OAuthWebSecurity.RegisterClient(new MailAccountClient("http://mailaccount.3cloud.com/OpenId?force_login=1"), "三宝邮箱", new Dictionary<string, object>());
            //#else
            OAuthWebSecurity.RegisterClient(new MailAccountClient(), "三宝邮箱", new Dictionary<string, object>());
            //#endif
            if (ComponentRegistry.Resolve<IApplication>().Settings["BaiduClientId"] != null)
            {
                OAuthWebSecurity.RegisterClient(new BaiduOAuthClient(), "百度", new Dictionary<string, object>());
            }

            OAuthWebSecurity.RegisterGoogleClient();

            if (ComponentRegistry.Resolve<IApplication>().Settings["WeiboClientId"] != null)
            {
                OAuthWebSecurity.RegisterClient(new WeiboOAuthClient(), "微博", new Dictionary<string, object>());
            }
            if (ComponentRegistry.Resolve<IApplication>().Settings["QQClientId"] != null)
            {
                OAuthWebSecurity.RegisterClient(new QQOAuthClient(), "QQ", new Dictionary<string, object>());
            }
            //OAuthWebSecurity.RegisterClient(new InovoutOAuthClient());
        }
    }
}